import {permissionList}  from '@/config/PermissionConfig'
import { ref, onMounted, useTemplateRef } from 'vue';
import {storeToRefs} from 'pinia'
import {type R} from "@/types/R"
import {request} from "@/utils/Request"
import {ElMessage, ElMessageBox} from "element-plus"
import {type PageInfo} from "@/types/PageInfo"
import {randomId} from "@/utils/IdUtils"
import { usePayOrderStore } from './PayOrderStore';
import { useApplicationStore } from '@/pages/application/applicationStore'
import type { PayOrderVO, PayBackAO, PayBackVO } from './PayOrderTypes';

export const usePayOrderHooks = function(){
    let payOrderStore = usePayOrderStore()
    let {
        selectPayOrderAO, payBackDialog, payBackAO, payBackOrderDialog, selectPayBackOrderAO
    } = storeToRefs(payOrderStore)

    let applicationStore = useApplicationStore();
    let {isCollapse} = storeToRefs(applicationStore)

    const reset = ()=>{
        selectPayOrderAO.value = {}
        select()
    }

    const backReset = ()=>{
        let payDtoId = selectPayBackOrderAO.value.payDtoId
        selectPayBackOrderAO.value = {}
        selectPayBackOrderAO.value.payDtoId = payDtoId
        backSelect()
    }

    onMounted(()=>{
        select()
        backSelect()
    })

    let selectLoading = ref<boolean>(false)
    let pageInfo = ref<PageInfo<PayOrderVO>>()
    const select = async ()=>{
        selectLoading.value = true
        // console.log(selectPayOrderAO.value.successTime)
        let r:R<PageInfo<PayOrderVO>> = await request.post("/pay/selectPayOrder",selectPayOrderAO.value)
        if(r.code == "success"){
            pageInfo.value = r.data
        }
        selectLoading.value = false
    }

    // 退款记录
    let backSelectLoading = ref<boolean>(false)
    let backPageInfo = ref<PageInfo<PayBackVO>>()
    const backSelect = async ()=>{
        backSelectLoading.value = true
        let r:R<PageInfo<PayBackVO>> = await request.post("/pay/selectPayBackOrder",selectPayBackOrderAO.value)
        if(r.code == "success"){
            backPageInfo.value = r.data
        }
        backSelectLoading.value = false
    }

    // 退款
    const willPayBack = (payDtoId:string)=>{
        payBackAO.value.payDtoId = payDtoId
        payBackDialog.value = true
    }

    const payBackCancel = ()=>{
        payBackDialog.value = false
    }

    const payBackSave = async()=>{
        let r:R<any> = await request.post("/pay/payBack", payBackAO.value)
        if(r.code == "success"){
            ElMessage.success("退款成功")
            select()
        }
        payBackDialog.value = false
    }

    const showPayBackOrder = (payDtoId:string)=>{
        payBackOrderDialog.value = true
        selectPayBackOrderAO.value.payDtoId = payDtoId
        backSelect()
    }

    return {
        isCollapse, selectPayOrderAO, reset, select,
        selectLoading, pageInfo, willPayBack, payBackDialog, 
        payBackAO, payBackCancel, payBackSave, payBackOrderDialog,
        showPayBackOrder, selectPayBackOrderAO, backSelectLoading, backPageInfo,
        backReset, backSelect
    }
}
